/**
 *
 */
package com.open.capacity;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.cache.annotation.EnableCaching;
import com.open.capacity.annotation.EnableLogging;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
* @author 作者 ftquan E-mail: 464725602@qq.com
* @version 创建时间：2018年4月5日 下午19:52:21
* 类说明
*/

@EnableLogging
@SpringBootApplication
@EnableCaching
@EnableScheduling
public class AuditCenterApp {

	public static void main(String[] args) {
		SpringApplication.run(AuditCenterApp.class, args);

		//随机端口启动
//		SpringApplication app = new SpringApplication(UserCenterApp.class);
//        app.addListeners(new PortApplicationEnvironmentPreparedEventListener());
//        app.run(args);

	}

	@Bean
	@ConditionalOnProperty(name = "server.ssl.key-store", havingValue = "true")
	public Connector connector(){
		Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
		connector.setScheme("http");
		connector.setPort(7000);
		connector.setSecure(false);
		connector.setRedirectPort(8443);
		return connector;
	}

	@Bean
	@ConditionalOnProperty(name = "server.ssl.key-store", havingValue = "true")
	public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){
		TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory(){
			@Override
			protected void postProcessContext(Context context) {
				SecurityConstraint securityConstraint=new SecurityConstraint();
				securityConstraint.setUserConstraint("CONFIDENTIAL");
				SecurityCollection collection=new SecurityCollection();
				collection.addPattern("/*");
				securityConstraint.addCollection(collection);
				context.addConstraint(securityConstraint);
			}
		};
		tomcat.addAdditionalTomcatConnectors(connector);
		return tomcat;
	}


}
